package com.by_man.presenter.module_health_records.image;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 查询本地数据库，取出所有的图片信息
 * Created by man on 2017-02-28.
 */
public class AllImages {
    // 存储本地所以图片组信息
    private HashMap<String, BucketImgList> bucketList = new HashMap<>();

    // 存储所有缓存图片路径
    private HashMap<String, String> thumbnaiList = new HashMap<>();

    // 通过这个对象访问数据库查找所有的图片
    static ContentResolver contentResolver;

    // 标记bucketList是否初始化完成
    private boolean hasInitBucketList = false;

    // 标记Thumbnailst是否初始化
    private boolean hasInitThumbnail = false;

    private static AllImages instance;

    private AllImages() {

    }

    public static AllImages getInstance() {
        if (null == instance) {
            instance = new AllImages();
        }
        return instance;
    }

    public void init(Context context) {
        if (null == contentResolver) {
            contentResolver = context.getContentResolver();
        }
    }

    // 查询本地数据，取得所有的图片信息，初始化bucketList
    private void buildBucketList() {

        // 将要获取的图片信息列
        String columns[] = new String[] {
                MediaStore.Images.Media.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                MediaStore.Images.Media.BUCKET_ID, MediaStore.Images.Media._ID
        };
        // 查询
        Cursor cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                columns,null,null,null);
        if (cursor.moveToFirst()) {
            int imgIdIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
            int imgPathIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            int bucketNameIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
            int bucketIdIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_ID);
            do {
                String path = cursor.getString(imgPathIndex);
                String bucketName = cursor.getString(bucketNameIndex);
                String bucketId = cursor.getString(bucketIdIndex);
                String imgId = cursor.getString(imgIdIndex);
                BucketImgList bucket = bucketList.get(bucketId);
                if (null == bucket) {       // 初始BucketImgList
                    bucket = new BucketImgList();
                    bucket.imgList = new ArrayList<>();
                    bucketList.put(bucketId, bucket);
                    bucket.bucketName = bucketName;     // 设定名字
                }
                // 信息记录输出，方便debug
                Log.d("ImgsInfo", "BucketId-->" + bucketId + ";BucketName-->"
                + bucketName + ";ImgPath-->" + path);
                // 加入这组图片信息
                ImageInfo imageInfo = new ImageInfo();
                imageInfo.setImagePath(path);
                imageInfo.setThumbnailPath(thumbnaiList.get(imgId));
                bucket.imgList.add(imageInfo);
                bucket.count++;
            } while (cursor.moveToNext());
        }
        cursor.close();

        hasInitBucketList = true;
    }

    // 返回所有图片信息
    public ArrayList<BucketImgList> getAllImgs(boolean refresh) {
        // 设定缓存
        getAllThumbnails(refresh);
        if (refresh || (!refresh && !hasInitBucketList))
            buildBucketList();
        ArrayList<BucketImgList> imgList = new ArrayList<>();
        Iterator<Map.Entry<String, BucketImgList>> itr = bucketList.entrySet().iterator();
        while (itr.hasNext()) {
            imgList.add(itr.next().getValue());
        }
        return imgList;
    }

    public void getAllThumbnails(boolean refresh) {
        if (refresh || !hasInitThumbnail) {
            Cursor cursor = contentResolver.query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                    new String[] {MediaStore.Images.Thumbnails.IMAGE_ID, MediaStore.Images.Thumbnails.DATA},
                    null, null, null);
            if (cursor.moveToFirst()) {
                int image_idIndex = cursor.getColumnIndex(MediaStore.Images.Thumbnails.IMAGE_ID);
                int dataIndex = cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA);
                do {
                    thumbnaiList.put("" + cursor.getInt(image_idIndex), cursor.getString(dataIndex));
                } while (cursor.moveToNext());
            }
        }
        hasInitThumbnail = true;
    }
}
